package com.liu.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liu.reggie.common.R;
import com.liu.reggie.dto.SetmealDto;
import com.liu.reggie.pojo.Category;
import com.liu.reggie.pojo.Dish;
import com.liu.reggie.pojo.Setmeal;
import com.liu.reggie.service.CategoryService;
import com.liu.reggie.service.SetmealDishService;
import com.liu.reggie.service.SetmealService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liustart
 * @create 2022-07-28 10:14
 */
@RequestMapping("/setmeal")
@RestController
public class SetmealController {
    @Autowired
    SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * CachePut：将方法返回值放入缓存
     * value：缓存的名称，每个缓存名称下面可以有多个key
     * key：缓存的key
     */
    @CacheEvict(value = "setmealCache",allEntries = true)
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        Page<SetmealDto> dtoPage = new Page<>();
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name!=null,Setmeal::getName,name);
        setmealService.page(setmealPage,queryWrapper);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        BeanUtils.copyProperties(setmealPage,dtoPage,"records");
        List<Setmeal> records =setmealPage.getRecords();
        List<SetmealDto> setmealDtos=new ArrayList<>();
        for (Setmeal record : records) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(record,setmealDto);
            Long categoryId = record.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (category!=null){
                setmealDto.setCategoryName(category.getName());
            }
            setmealDtos.add(setmealDto);
        }
        dtoPage.setRecords(setmealDtos);
        return R.success(dtoPage);

    }

    /**
     * CacheEvict：清理指定缓存
     * value：缓存的名称，每个缓存名称下面可以有多个key
     * key：缓存的key
     */
    @CacheEvict(value = "setmealCache",allEntries = true)
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.removeWithDish(ids);
        return R.success("套餐数据删除成功");
    }
    /**
     * Cacheable：在方法执行前spring先查看缓存中是否有数据，如果有数据，则直接返回缓存数据；若没有数据，调用方法并将方法返回值放到缓存中
     * value：缓存的名称，每个缓存名称下面可以有多个key
     * key：缓存的key
     * condition：条件，满足条件时才缓存数据
     * unless：满足条件则不缓存
     */

    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(setmeal.getCategoryId()!=null, Setmeal::getCategoryId,setmeal.getCategoryId());
        wrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(wrapper);
        return R.success(list);
    }

    @PostMapping("/status/1")
    @ApiOperation("启售状态")
    public R<String>  start(@RequestParam List<Long> ids){
        for (Long id : ids) {
            LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Setmeal::getId,id);
            Setmeal setmeal = setmealService.getById(id);
            setmeal.setStatus(1);
            setmealService.update(setmeal,queryWrapper);
        }
        return R.success("更改成功");
    }

    @PostMapping("/status/0")
    @ApiOperation("停止售状态")
    public R<String> end(@RequestParam List<Long> ids){
        for (Long id : ids) {
            LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Setmeal::getId,id);
            Setmeal setmeal = setmealService.getById(id);
            setmeal.setStatus(0);
            setmealService.update(setmeal,queryWrapper);
        }
        return R.success("更改成功");
    }

    @PutMapping
    @ApiOperation("修改套餐接口")
    public R<String> update(@RequestBody Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getId,setmeal.getId());
        setmealService.update(setmeal,queryWrapper);
        return R.success("修改成功");
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public R<SetmealDto> get(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.getByIdWithDish(id);
        return R.success(setmealDto);
    }

}
